package com.tuiyun.jxgl.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;
import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.jxgl.dao.StudentHome;

public class ResourceAction{
	
	private DriverHome dh;
	private StudentHome sh;
	
	private File fileupload;//上传的文件
	private String fileuploadFileName;//文件的名称  如上传的文件是a.png 则fileuploadFileName值为"a.png"
	private String fileuploadContentType;//文件的类型  如上传的是png格式的图片，则fileuploadContentType值为"image/png"	
	private String savePath;//文件的保存位置
	
	
	@Transactional
	public String uploadFile() throws Exception {
		
		Subject subject = SecurityUtils.getSubject();
		if(subject != null && subject.isAuthenticated())
		{			
			String absolutePath = ServletActionContext.getServletContext().getRealPath("");// 获取项目根路径
			//文件路径
			String path = absolutePath + File.separator + this.savePath + File.separator;
			//创建路径，如果目录不存在，则创建
			File file = new File(path);
			if(!file.exists()) {
				file.mkdirs();
			}
			String uuid = java.util.UUID.randomUUID().toString();
			//文件路径+文件名
			path += uuid;
			//1.构建输入流  
			FileInputStream fis = new FileInputStream(fileupload);
			//2.构建输出流  
			FileOutputStream fos = new FileOutputStream(path);
			//3.通过字节写入输出流  
			try {
				byte[] buf = new byte[1024];
				int len = 0;
				while ((len = fis.read(buf)) > 0) {
					fos.write(buf, 0, len);
				}				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				fis.close();
				fos.close();
			}
			Object o = subject.getPrincipal();
			if(o instanceof Student){
				Student s = (Student)o;
				if(s.getAvatar() != null){
					File f = new File(absolutePath + File.separator + this.savePath + File.separator+s.getAvatar());
					if(f.exists())
						f.delete();
				}
				s.setAvatar(uuid);
				sh.attachDirty(s);
			}else{
				Driver d = (Driver) o;
				if(d.getAvatar() != null){
					File f = new File(absolutePath + File.separator + this.savePath + File.separator+d.getAvatar());
					if(f.exists())
						f.delete();
				}
				d.setAvatar(uuid);
				dh.attachDirty(d);
			}
			return "success";
		}
		else
			return "error";		
          
	}
	
	public DriverHome getDh() {
		return dh;
	}

	public void setDh(DriverHome dh) {
		this.dh = dh;
	}

	public StudentHome getSh() {
		return sh;
	}

	public void setSh(StudentHome sh) {
		this.sh = sh;
	}

	public File getFileupload() {
		return fileupload;
	}
	public void setFileupload(File fileupload) {
		this.fileupload = fileupload;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getFileuploadFileName() {
		return fileuploadFileName;
	}
	public void setFileuploadFileName(String fileuploadFileName) {
		this.fileuploadFileName = fileuploadFileName;
	}
	public String getFileuploadContentType() {
		return fileuploadContentType;
	}
	public void setFileuploadContentType(String fileuploadContentType) {
		this.fileuploadContentType = fileuploadContentType;
	}
}
